//引入express模块
const express=require('express');
//引入连接池pool.js
const pool=require('../pool.js');
console.log(pool);//测试是否成功引入连接池
//创建路由器对象
const r=express.Router();


//1.登录接口
//http://127.0.0.1:8080/v1/users/login
r.post('/login',(req,res,next)=>{
//1.1获取post传递的参数（body）
var obj=req.body;
//console.log(obj);//此时cmd中显示body的各项值，但是数据库并没有添加内容
//1.2判断各项值是否为空  !!!一定加上return,否则cmd出现错误行
if (!obj.uname)
{
res.send({code:401,msg:'手机号不能为空'});
return;//阻止往后执行
}
if (!obj.upwd)
{
res.send({code:402,msg:'密码不能为空'});
return;
}
//SQL命令
pool.query('select * from tuhu_user where uname=? and upwd=?',[obj.uname,obj.upwd],(err,result)=>{
if (err)
{
	next(err);
	return;
}
console.log(result,'长度',result.length)
		if(result.length===0){
			res.send({code:201,msg:'登录失败'});
		}else{
			res.send({code:200,msg:'登录成功',data:result});
		}
//console.log(result);

});

});



//-----------------------登录结束----------------

//2.注册用户（post /reg）
//接口地址：http://127.0.0.1:8080/v1/users/reg
//请求方式：post
r.post('/reg',(req,res,next)=>{
	//第3步
//1.1获取post传递的参数（body）
var obj=req.body;
//console.log(obj);//此时cmd中显示body的各项值，但是数据库并没有添加内容
//第4步  1.2判断各项值是否为空，手机号码格式是否输入正确等  !!!一定加上return,否则cmd出现错误行
if (!obj.uname)
{
res.send({code:401,msg:'用户名不能为空'});
return;//阻止往后执行
}
if (!obj.upwd)
{
res.send({code:402,msg:'密码不能为空'});
return;
}
if (!obj.repwd)
{
res.send({code:403,msg:'确认密码不能为空'});
return;
}

if (!obj.message)
{
res.send({code:404,msg:'验证码不能为空'});
return;
}
if (!obj.phone)
{
res.send({code:405,msg:'电话不能为空'});
return;
}


//第5步，前面测试成功后创建连接池pool.js，用于导出，可在不同模块下使用，哪一个需要用到连接池哪一个路由器就引入
//同时引入连接池模块pool.js



//第6步:执行sql命令
pool.query('insert into tuhu_user set?',[obj],(err,result)=>{
if (err)
{
	next(err);
	return;
}
if(result.affectedRows===0){
			res.send({code:201,msg:'用户添加失败'});
		}else{
			res.send({code:200,msg:'用户添加成功',data:result});
		}
});
});





//-------------------------注册用户结束-------------------



//3.删除接口
//http://127.0.0.1:8080/v1/users/delete/t_chuqin/6   //路由传参    建议查询，删除
r.delete('/delete/tuhu_shopping_car/:cid',(req,res,next)=>{


//console.log(req.params);
//SQL命令
pool.query('delete from tuhu_shopping_car where cid=? ',[req.params.cid],(err,result)=>{
if (err)
	{
		next(err);//将错误交给下一个中间件
		return;//阻止往后执行
	}
	console.log(result);
	//成功的结果是对象
	//如果对象下的affectedRows的值为0说明删除失败，否则删除成功
	if (result.affectedRows===0)
	{
		res.send({code:201,msg:'删除失败'});
	}else{
	res.send({code:200,msg:'删除成功'});
	}

});

});
//3.删除接口
//http://127.0.0.1:8080/v1/users/delete/t_chuqin/6   //路由传参    建议查询，删除
r.delete('/delete/tuhu_order/:oid',(req,res,next)=>{


	//console.log(req.params);
	//SQL命令
	pool.query('delete from tuhu_order where oid=? ',[req.params.oid],(err,result)=>{
	if (err)
		{
			next(err);//将错误交给下一个中间件
			return;//阻止往后执行
		}
		console.log(result);
		//成功的结果是对象
		//如果对象下的affectedRows的值为0说明删除失败，否则删除成功
		if (result.affectedRows===0)
		{
			res.send({code:201,msg:'删除失败'});
		}else{
		res.send({code:200,msg:'删除成功'});
		}
	
	});
	
	});
//-----------------------删除结束---------------------------
//4.修改接口
//http://127.0.0.1:8080/v1/users/update
r.put('/update',(req,res,next)=>{
console.log(req.body);
pool.query('update t_organization set? where id=? ',[req.body,req.body.id],(err,result)=>{//set=req.body
if (err)                                                                             //id=req.body.id
{
	next(err);
	return;
}
if (result.affectedRows===0)
{
	res.send({code:201,msg:'修改失败'});
}else{
    res.send({code:200,msg:'修改成功'});
}

});

});

//-----------------------修改结束-----------------------


//5.查询轮播图表  

r.get('/select/tuhu_banner',(req,res)=>{
	//console.log(req.params)
	pool.query('select * from tuhu_banner ',[req.params.bid],(err,result)=>{
		if(err) throw err;
		console.log(result,'长度：',result.length);
		if(result.length===0){
		res.send({code:201,msg:'查询失败'});
		}else{
		res.send({code:200,msg:'查询成功',data:result});
		}
	})
	
})
//6.查询接口  
r.get('/select/ymt_bimai_listleft',(req,res)=>{
	//console.log(req.params)
	pool.query('select * from ymt_bimai_listleft ',[req.params.bimai_lid],(err,result)=>{
		if(err) throw err;
		console.log(result,'长度：',result.length);
		if(result.length===0){
		res.send({code:201,msg:'查询失败'});
		}else{
		res.send({code:200,msg:'查询成功',data:result});
		}
	})
	
})
//7.查询接口
r.get('/select/ymt_bimai_listright',(req,res)=>{
	console.log(req.params)
	pool.query('select * from ymt_bimai_listright',[req.params.bimai_rid],(err,result)=>{
		if(err) throw err;
		console.log(result,'长度：',result.length);
		if(result.length===0){
		res.send({code:201,msg:'查询失败'});
		}else{
		res.send({code:200,msg:'查询成功',data:result});
		}
	})
	
})
//8.查询接口  分类大图片
//http://127.0.0.1:8080/v1/users/ymt_bimai_listright
r.get('/select/ymt_fenlei_list',(req,res)=>{
	console.log(req.params)
	pool.query('select * from ymt_fenlei_list',[req.params.fenlei_rid],(err,result)=>{
		if(err) throw err;
		console.log(result,'长度：',result.length);
		if(result.length===0){
		res.send({code:201,msg:'查询失败'});
		}else{
		res.send({code:200,msg:'查询成功',data:result});
		}
	})
	
})
//10.查询接口  分类大图片
//http://127.0.0.1:8080/v1/users/ymt_bimai_listright
r.get('/select/ymt_product_fenlei',(req,res)=>{

	console.log(req.params)
	pool.query('select * from ymt_product_fenlei',[req.params.fen_rid],(err,result)=>{
		if(err) throw err;
		console.log(result,'长度：',result.length);
		if(result.length===0){
		res.send({code:201,msg:'查询失败'});
		}else{
		res.send({code:200,msg:'查询成功',data:result});
		}
	})
	
})

//12.
r.get('/select/tuhu_shop',(req,res)=>{

	console.log(req.query)
	pool.query('select * from tuhu_shop where sid=?',[req.query.sid],(err,result)=>{
		if(err) throw err;
		console.log(result,'长度：',result.length);
		if(result.length===0){
		res.send({code:201,msg:'查询失败'});
		}else{
		res.send({code:200,msg:'查询成功',data:result[0]});
		}
	})
	
})
//10.查询详情  搜索查询
//http://127.0.0.1:8080/v1/users/ymt_bimai_listright
r.get('/select/ymt_product',(req,res)=>{

	console.log(req.query)
	pool.query('select * from ymt_product where rid=?',[req.query.rid],(err,result)=>{
		if(err) throw err;
		console.log(result,'长度：',result.length);
		if(result.length===0){
		res.send({code:201,msg:'查询失败'});
		}else{
		res.send({code:200,msg:'查询成功',data:result[0]});
		}
	})
	
})
//10.搜索功能接口
//http://127.0.0.1:8080/v1/users/ymt_product
r.get('/select/ymt_product',(req,res)=>{

	console.log(req.query)
	pool.query('select * from ymt_product where title like?',['%'+req.query.title+'%'],(err,result)=>{
		if(err) throw err;
		console.log(result,'长度：',result.length);
		if(result.length===0){
		res.send({code:201,msg:'查询失败'});
		}else{
		res.send({code:200,msg:'查询成功',data:result});
		}
	})
	
})
//9.查询接口  分类小图片
// select ename,dname from emp inner join dept on deptId=did;
//http://127.0.0.1:8888/v1/users/ymt_bimai_listright
r.get('/select/ymt_fenlei_listlg',(req,res)=>{
	let data=[];
//inner join ymt_fenlei_list on ymt_fenlei_listlg.title_img=ymt_fenlei_list.title_img
	console.log('req.params:',req.params)
	pool.query('select * from ymt_bimai_listright',[req.params.bimai_rid],(err,result)=>{
		if(err) throw err;
		console.log(result,'长度：',result.length);
		if(result.length===0){
		res.send({code:201,msg:'查询失败'});
		}else{
			data.push(result);
			pool.query('select * from ymt_fenlei_listlg ',[req.params.title_img],(err,result)=>{
				if(err) throw err;
				console.log(result,'长度：',result.length);
				if(result.length===0){
				res.send({code:201,msg:'查询失败'});
				}else{
							data.push(result);
							pool.query('select * from ymt_fenlei_list ',[req.params.title_img],(err,result)=>{
								if(err) throw err;
								console.log(result,'长度：',result.length);
								if(result.length===0){
								res.send({code:201,msg:'查询失败'});
								}else{
									
									data.push(result);
									pool.query('select * from ymt_lunbo_first ',[req.params.title],(err,result)=>{
										if(err) throw err;
										console.log(result,'长度：',result.length);
										if(result.length===0){
										res.send({code:201,msg:'查询失败'});
										}else{
											
											data.push(result);
											pool.query('select * from ymt_lunbo_two ',[req.params.title],(err,result)=>{
												if(err) throw err;
												console.log(result,'长度：',result.length);
												if(result.length===0){
												res.send({code:201,msg:'查询失败'});
												}else{
													
													data.push(result);
													pool.query('select * from ymt_lunbo_three ',[req.params.title],(err,result)=>{
														if(err) throw err;
														console.log(result,'长度：',result.length);
														if(result.length===0){
														res.send({code:201,msg:'查询失败'});
														}else{
															
															data.push(result);
															pool.query('select * from ymt_lunbo_four ',[req.params.title],(err,result)=>{
																if(err) throw err;
																console.log(result,'长度：',result.length);
																if(result.length===0){
																res.send({code:201,msg:'查询失败'});
																}else{
																	
																	data.push(result);
																	pool.query('select * from ymt_lunbo_five ',[req.params.title],(err,result)=>{
																		if(err) throw err;
																		console.log(result,'长度：',result.length);
																		if(result.length===0){
																		res.send({code:201,msg:'查询失败'});
																		}else{
																			
																			data.push(result);
																			// 获取客户端传递的cid参数
																				let cid = req.query.cid;

																				// 获取客户端传递的page参数
																				let page = req.query.page? req.query.page : 1;

																				// 存储每页显示的记录数
																				let pagesize = 10;

																				// 通过公式来计算从第几条记录开始返回
																				let offset = (page - 1) * pagesize;

																				// 用于存储获取到的总记录数
																				let rowcount;
																			pool.query('select * from ymt_product limit 0,10; ',[req.params.title,req.params.rid],(err,result)=>{
																				if(err) throw err;
																				console.log(result,'长度：',result.length);
																				if(result.length===0){
																				res.send({code:201,msg:'查询失败'});
																				}else{
																					
																					data.push(result);
																					pool.query('select * from ymtlunbo ',[req.params.title],(err,result)=>{
																						if(err) throw err;
																						console.log(result,'长度：',result.length);
																						if(result.length===0){
																						res.send({code:201,msg:'查询失败'});
																						}else{
																							
																							data.push(result);
																								res.send({code:200,msg:'查询成功',data:data});
																
																						}
																					})
														
																				}
																			})
												
																		}
																	})
										
																}
															})
								
														}
													})
						
												}
											})
				
										
				
										}
									})
									
		
								}
							})
							
				}
			})
		}
	})




	
})


//导出路由器
module.exports=r;